package com.lanxin.zc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanxin.bx.entity.BxInformal;
import com.lanxin.bx.entity.BxInformalaccessary;
import com.lanxin.bx.entity.BxInformaldetail;
import com.lanxin.bx.entity.BxInformaltype;
import com.lanxin.bx.service.IBxInformalService;
import com.lanxin.bx.service.IBxInformalaccessaryService;
import com.lanxin.bx.service.IBxInformaldetailService;
import com.lanxin.bx.service.IBxInformaltypeService;
import com.lanxin.hr.entity.JcBasicdatavalue;
import com.lanxin.hr.entity.ZzDepartment;
import com.lanxin.hr.entity.ZzEmployee;
import com.lanxin.hr.service.IJcBasicdatavalueService;
import com.lanxin.hr.service.IZzDepartmentService;
import com.lanxin.hr.service.IZzEmployeeService;
import com.lanxin.judge.entity.LcFlow;
import com.lanxin.judge.service.ILcFlowService;
import com.lanxin.zc.entity.*;
import com.lanxin.zc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-07-16
 */
@Controller
@RequestMapping("/zc-assetpurchaseapplydetails")
public class ZcAssetpurchaseapplydetailsController {

    @Autowired
    private IZcAssetpurchaseapplyService zcAssetpurchaseapplyService;
    @Autowired
    private IZcAssetpurchaseapplydetailsService zzAssetpurchaseapplydetailsService;
    @Autowired
    private IZcAssetbaojiaService zzAssetbaojiaService;
    @Autowired
    private IZzEmployeeService zzEmployeeService;
    @Autowired
    private IZzDepartmentService zzDepartmentService;
    @Autowired
    private ILcFlowService lcFlowService;
    @Autowired
    private IBxInformalService bxInformalService;
    @Autowired
    IBxInformaldetailService bxInformaldetailService;
    @Autowired
    IBxInformalaccessaryService bxInformalaccessaryService;
    @Autowired
    IBxInformaltypeService bxInformaltypeService;
    @Autowired
    private IJcBasicdatavalueService jcBasicdatavalueService;
    @Autowired
    private IZcAssetService zcAssetService;
    @Autowired
    private IZcAssetmoveapplyService zcAssetmoveapplyService;
    @Autowired
    private IZcAssetmovedetailsService zcAssetmovedetailsService;

    @RequestMapping("/zcAssetmoveapplyInfo")
    public ModelAndView zcAssetmoveapplyInfo(@RequestParam(value = "oid") Integer oid) {
        ModelAndView modelAndView = new ModelAndView();

        // 1. 查询主表信息（移动申请单）
        ZcAssetmoveapply main = zcAssetmoveapplyService.getById(oid);
        modelAndView.addObject("main", main);

        // 2. 查询明细列表（移动明细）
        List<ZcAssetmovedetails> details = zcAssetmovedetailsService.list(
                new QueryWrapper<ZcAssetmovedetails>()
                        .eq("parentid", oid)
        );
        //3.资产名称
        for (ZcAssetmovedetails detail : details) {
            // 查询资产信息
            if (detail.getAssetId() != null) {
                ZcAsset asset = zcAssetService.getById(detail.getAssetId());
                detail.setAsset(asset);

                // 查询原保管人信息
                if (asset.getKeeper() != null) {
                    ZzEmployee keeper = zzEmployeeService.getById(asset.getKeeper());
                    detail.setKeeper(keeper);
                }

                // 查询原存放地信息
                if (asset.getDepositary() != null) {
                    JcBasicdatavalue storage = jcBasicdatavalueService.getById(asset.getDepositary());
                    detail.setStorage(storage);
                }
            }

            // 查询移动目的地信息
            if (detail.getTargetPlace() != null) {
                JcBasicdatavalue targetPlace = jcBasicdatavalueService.getById(detail.getTargetPlace());
                detail.setTargetPlaceObj(targetPlace);
            }
        }

        // 4. 查询人员和组织信息
        // 创建人
        if (main.getCreator() != null) {
            ZzEmployee creator = zzEmployeeService.getById(main.getCreator());
            modelAndView.addObject("creatorName", creator != null ? creator.getEmpName() : "");
        }
        // 申请人名称
        if (main.getProposer() != null) {
            ZzEmployee proposer = zzEmployeeService.getById(main.getProposer());
            modelAndView.addObject("proposerName", proposer != null ? proposer.getEmpName() : "");
        }
        // 部门名称
        if (main.getDeptid() != null) {
            ZzDepartment dept = zzDepartmentService.getById(main.getDeptid());
            modelAndView.addObject("deptName", dept != null ? dept.getDeptName() : "");
        }
        // 移动事由
        if (main.getMoveReason() != null) {
            JcBasicdatavalue reason = jcBasicdatavalueService.getById(main.getMoveReason());
            modelAndView.addObject("reasonName", reason != null ? reason.getName() : "");
        }
        // 流程名称
        if (main.getFlowid() != null) {
            LcFlow flow = lcFlowService.getById(main.getFlowid());
            modelAndView.addObject("flowName", flow != null ? flow.getFlowName() : "");
        }

        modelAndView.addObject("details", details);
        modelAndView.setViewName("zc/zcassetmoveapply/zcAssetmoveapplyInfo");

        return modelAndView;
    }

    @RequestMapping("/zcAssetpurchaseapplyInfo")
    public ModelAndView zcAssetpurchaseapplyInfo(@RequestParam(value = "oid") Integer oid) {
        ModelAndView modelAndView = new ModelAndView();

        // 1. 查询主表信息（购买申请单）
        ZcAssetpurchaseapply main = zcAssetpurchaseapplyService.getById(oid);
        modelAndView.addObject("main", main);

        // 2. 查询明细列表（购买明细）
        List<ZcAssetpurchaseapplydetails> details = zzAssetpurchaseapplydetailsService.list(
                new QueryWrapper<ZcAssetpurchaseapplydetails>()
                        .eq("parentid", oid)
        );

        // 3. 为每个明细查询对应的报价列表
        for (ZcAssetpurchaseapplydetails detail : details) {
            List<ZcAssetbaojia> baojiaList = zzAssetbaojiaService.list(
                    new QueryWrapper<ZcAssetbaojia>()
                            .eq("parentid", detail.getOid())
            );
            detail.setBaojiaList(baojiaList); // 需要在明细实体类中添加此属性
        }
        //创建人
        if (main.getCreator() != null) {
            ZzEmployee creator = zzEmployeeService.getById(main.getCreator());
            modelAndView.addObject("creatorName", creator != null ? creator.getEmpName() : "");
        }
        //申请人名称
        if (main.getProposer() != null) {
            ZzEmployee proposer = zzEmployeeService.getById(main.getProposer());
            modelAndView.addObject("proposerName", proposer != null ? proposer.getEmpName() : "");
        }
        //部门名称
        if (main.getDeptid() != null) {
            ZzDepartment dept = zzDepartmentService.getById(main.getDeptid()); // 需注入deptService
            modelAndView.addObject("deptName", dept != null ? dept.getDeptName() : "");
        }

        modelAndView.addObject("details", details);
        modelAndView.setViewName("zc/zcassetpurchaseapply/zcassetpurchaseapplyinfo");

        return modelAndView;
    }

    @RequestMapping("/bxInformalInfo")
    public ModelAndView bxInformalInfo(
            @RequestParam(value = "oid") Integer oid) {

        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<BxInformal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("oid", oid);
        BxInformal bxInformal = bxInformalService.getOne(queryWrapper);
        // 转换 createDate（LocalDateTime -> Date）
        if (bxInformal.getCreateDate() != null) {
            Date createDate = Date.from(bxInformal.getCreateDate().atZone(ZoneId.systemDefault()).toInstant());
            modelAndView.addObject("createDate", createDate); // 传递转换后的 Date 对象
        }

// 转换 submitDate
        if (bxInformal.getSubmitDate() != null) {
            Date submitDate = Date.from(bxInformal.getSubmitDate().atZone(ZoneId.systemDefault()).toInstant());
            modelAndView.addObject("submitDate", submitDate);
        }

// 转换 bookDate
        if (bxInformal.getBookDate() != null) {
            Date bookDate = Date.from(bxInformal.getBookDate().atZone(ZoneId.systemDefault()).toInstant());
            modelAndView.addObject("bookDate", bookDate);
        }

        QueryWrapper<BxInformaldetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("parentid", oid);
        List<BxInformaldetail> bxInformaldetails = bxInformaldetailService.list(queryWrapper1);
        List<BxInformaltype> bxTypes = bxInformaltypeService.list(); // 需注入 BxTypeService
        Map<Integer, String> typeMap = new HashMap<>();
        for (BxInformaltype type : bxTypes) {
            typeMap.put(type.getOid(), type.getName()); // 类型ID作为key，名称作为value
        }
        modelAndView.addObject("typeMap", typeMap); // 传递映射到前端



        // 2.1 审批流程名称
        if (bxInformal.getFlowid() != null) {
            LcFlow lcFlow = lcFlowService.getById(bxInformal.getFlowid());
            modelAndView.addObject("flowName", lcFlow != null ? lcFlow.getFlowName() : ""); // 假设LcFlow有flowName字段
        }

        // 2.2 创建人名称
        if (bxInformal.getCreator() != null) {
            ZzEmployee creator = zzEmployeeService.getById(bxInformal.getCreator());
            modelAndView.addObject("creatorName", creator != null ? creator.getEmpName() : "");
        }

        // 2.3 申请人名称
        if (bxInformal.getProposer() != null) {
            ZzEmployee proposer = zzEmployeeService.getById(bxInformal.getProposer());
            modelAndView.addObject("proposerName", proposer != null ? proposer.getEmpName() : "");
        }

        // 2.4 部门名称
        if (bxInformal.getDeptid() != null) {
            ZzDepartment dept = zzDepartmentService.getById(bxInformal.getDeptid()); // 需注入deptService
            modelAndView.addObject("deptName", dept != null ? dept.getDeptName() : "");
        }

        // 2.5 领款人名称
        if (bxInformal.getPayee() != null) {
            ZzEmployee payee = zzEmployeeService.getById(bxInformal.getPayee());
            modelAndView.addObject("payeeName", payee != null ? payee.getEmpName() : "");
        }

        // 2.6 领款登记人名称
        if (bxInformal.getBooker() != null) {
            ZzEmployee booker = zzEmployeeService.getById(bxInformal.getBooker());
            modelAndView.addObject("bookerName", booker != null ? booker.getEmpName() : "");
        }


        QueryWrapper<BxInformalaccessary> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("parentid", oid);
        List<BxInformalaccessary> bxInformalaccessaries = bxInformalaccessaryService.list(queryWrapper2);

        modelAndView.addObject("bxInformal", bxInformal);
        modelAndView.addObject("bxInformaldetails", bxInformaldetails);
        modelAndView.addObject("bxInformalaccessaries", bxInformalaccessaries);
        modelAndView.setViewName("bx/allinformal/bxinformalinfo");

        return modelAndView;
    }

    @RequestMapping("/towhereinfo")
    public String towhereinfo(
            @RequestParam Integer oid,
            @RequestParam(required = false) String doctype
    ) {
        // 根据单据类型(doctype)跳转到不同的详情页
        if ("GMD".equals(doctype)) {
            // 资产购买申请单 (GMD) - 跳转到资产购买详情页
            return "redirect:/zc-assetpurchaseapplydetails/zcAssetpurchaseapplyInfo?oid=" + oid;
        } else if("BXD".equals(doctype)) {
            // 报销单 (BXD) - 跳转到报账单详情页
            return "redirect:/zc-assetpurchaseapplydetails/bxInformalInfo?oid=" + oid;
        } else if ("YDD".equals(doctype)) {
            return "redirect:/zc-assetpurchaseapplydetails/zcAssetmoveapplyInfo?oid=" + oid;
        } else {
            return null;
        }
    }


}
